﻿using LinqToAmazonSL.AWS;
using System.Globalization;

namespace LinqToAmazonSL
{
    public class AmazonDVD : AmazonItem
    {
        #region IAmazonItem Members

        public string Actor { get; set; }
        public string AspectRatio { get; set; }
        public string Audience { get; set; }
        public string Brand { get; set; }
        public string Director { get; set; }
        public string Format { get; set; }
        public string ItemDimensions { get; set; } 
        public string Label { get; set; }
        public string Languages { get; set; }
        public string MPN { get; set; }
        public string NumberOfDiscs { get; set; }
        public string OriginalReleaseDate { get; set; }
        public string RegionCode { get; set; }
        public string RunningTime { get; set; }
        public string TheatricalReleaseDate { get; set; }
        public string UPC { get; set; }

        #endregion

        //Function to save the results in the "AmazonDVD"
        public override void LoadFromAmazonResultItem(Item resultItem)
        {
            Actor = string.Empty;
            Condition = string.Empty;
            Director = string.Empty;
            Format = string.Empty;
            Image = null;
            ItemDimensions = string.Empty;
            Languages = string.Empty;
            Price = string.Empty;
            PackageDimensions = string.Empty;
            RunningTime = string.Empty;

            if (resultItem.ItemAttributes.Actor != null)
            {
                if (resultItem.ItemAttributes.Actor.Length > 0)
                {
                    int index = 0;
                    while (index < resultItem.ItemAttributes.Actor.Length - 1)
                    {
                        Actor += resultItem.ItemAttributes.Actor[index] + ", ";
                        index++;
                    }
                    Actor += resultItem.ItemAttributes.Actor[index];
                }
            }
            DetailPage = resultItem.DetailPageURL ?? string.Empty;
            AspectRatio = resultItem.ItemAttributes.AspectRatio ?? string.Empty;          
            Audience = resultItem.ItemAttributes.AudienceRating ?? string.Empty;
            Binding = resultItem.ItemAttributes.Binding ?? string.Empty;
            Brand = resultItem.ItemAttributes.Brand ?? string.Empty;
            if (resultItem.ItemAttributes.Director != null)
            {
                if (resultItem.ItemAttributes.Director.Length > 0)
                {
                    int index = 0;
                    while (index < resultItem.ItemAttributes.Director.Length - 1)
                    {
                        Director += resultItem.ItemAttributes.Director[index] + ", ";
                        index++;
                    }
                    Director += resultItem.ItemAttributes.Director[index];
                }
            }
            EAN = resultItem.ItemAttributes.EAN ?? string.Empty;
            if (resultItem.ItemAttributes.Format != null)
            {
                if (resultItem.ItemAttributes.Format.Length > 0)
                {
                    int index = 0;
                    while (index < resultItem.ItemAttributes.Format.Length - 1)
                    {
                        Format += resultItem.ItemAttributes.Format[index] + ", ";
                        index++;
                    }
                    Format += resultItem.ItemAttributes.Format[index];
                }
            }
            if (resultItem.ItemAttributes.ItemDimensions != null)
            {
                if (resultItem.ItemAttributes.ItemDimensions.Height != null)
                    ItemDimensions += "Height: " + resultItem.ItemAttributes.ItemDimensions.Height.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.ItemDimensions.Width != null)
                    ItemDimensions += "Width: " + resultItem.ItemAttributes.ItemDimensions.Width.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.ItemDimensions.Length != null)
                    ItemDimensions += "Length: " + resultItem.ItemAttributes.ItemDimensions.Length.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.ItemDimensions.Weight != null)
                    ItemDimensions += "Weight: " + resultItem.ItemAttributes.ItemDimensions.Weight.Value.ToString(CultureInfo.CurrentCulture);
            }
            Label = resultItem.ItemAttributes.Label ?? string.Empty;
            if (resultItem.ItemAttributes.Languages != null)
            {
                if (resultItem.ItemAttributes.Languages.Length > 0)
                {
                    int index = 0;
                    while (index < resultItem.ItemAttributes.Languages.Length)
                    {
                        Languages += "(Name= " + resultItem.ItemAttributes.Languages[index].Name + 
                                    ", Type= " + resultItem.ItemAttributes.Languages[index].Type + 
                                    ", AudioFormat= " + resultItem.ItemAttributes.Languages[index].AudioFormat + ")";
                        if (index < resultItem.ItemAttributes.Languages.Length - 1)
                            Languages += ", ";
                        index++;
                    }
                }
            }
            if (resultItem.OfferSummary != null && resultItem.OfferSummary.LowestUsedPrice != null)
            {
                TotalUsed = resultItem.OfferSummary.TotalUsed;
                LowestUsedPrice = resultItem.OfferSummary.LowestUsedPrice.FormattedPrice;
            }
            if (resultItem.OfferSummary != null && resultItem.OfferSummary.LowestNewPrice != null)
            {
                TotalNew = resultItem.OfferSummary.TotalNew;
                LowestNewPrice = resultItem.OfferSummary.LowestNewPrice.FormattedPrice;
            }
            if (resultItem.OfferSummary != null && resultItem.OfferSummary.LowestCollectiblePrice != null)
            {
                TotalCollectible = resultItem.OfferSummary.TotalCollectible;
                LowestCollectiblePrice = resultItem.OfferSummary.LowestCollectiblePrice.FormattedPrice;
            }
            if (resultItem.ItemAttributes.ListPrice != null && resultItem.ItemAttributes.ListPrice.FormattedPrice != null)
                Price = resultItem.ItemAttributes.ListPrice.FormattedPrice;
            
            Manufacturer = resultItem.ItemAttributes.Manufacturer ?? string.Empty;
            MPN = resultItem.ItemAttributes.MPN ?? string.Empty;
            NumberOfDiscs = resultItem.ItemAttributes.NumberOfDiscs ?? string.Empty;
            NumberOfItems = resultItem.ItemAttributes.NumberOfItems ?? string.Empty;
            OriginalReleaseDate = resultItem.ItemAttributes.OriginalReleaseDate ?? string.Empty;
            if (resultItem.ItemAttributes.PackageDimensions != null)
            {
                if (resultItem.ItemAttributes.PackageDimensions.Height != null)
                    PackageDimensions += "Height: " + resultItem.ItemAttributes.PackageDimensions.Height.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.PackageDimensions.Width != null)
                    PackageDimensions += "Width: " + resultItem.ItemAttributes.PackageDimensions.Width.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.PackageDimensions.Length != null)
                    PackageDimensions += "Length: " + resultItem.ItemAttributes.PackageDimensions.Length.Value.ToString(CultureInfo.CurrentCulture) + ", ";
                if (resultItem.ItemAttributes.PackageDimensions.Weight != null)
                    PackageDimensions += "Weight: " + resultItem.ItemAttributes.PackageDimensions.Weight.Value.ToString(CultureInfo.CurrentCulture);
            }
            ProductTypeName = resultItem.ItemAttributes.ProductTypeName ?? string.Empty;
            Publisher = resultItem.ItemAttributes.Publisher ?? string.Empty;
            RegionCode = resultItem.ItemAttributes.RegionCode ?? string.Empty;
            ReleaseDate = resultItem.ItemAttributes.ReleaseDate ?? string.Empty;
            if (resultItem.ItemAttributes.RunningTime != null)
                RunningTime += resultItem.ItemAttributes.RunningTime.Value + " " + resultItem.ItemAttributes.RunningTime.Units;
            Studio = resultItem.ItemAttributes.Studio ?? string.Empty;
            TheatricalReleaseDate = resultItem.ItemAttributes.TheatricalReleaseDate ?? string.Empty;
            Title = resultItem.ItemAttributes.Title ?? string.Empty;
            UPC = resultItem.ItemAttributes.UPC ?? string.Empty;
            if (resultItem.LargeImage != null)
            {
                Image = new Image();
                Image.Height = resultItem.LargeImage.Height;
                Image.Width = resultItem.LargeImage.Width;
                Image.URL = resultItem.LargeImage.URL;
            }
            else if (resultItem.MediumImage != null)
            {
                Image = new Image();
                Image.Height = resultItem.MediumImage.Height;
                Image.Width = resultItem.MediumImage.Width;
                Image.URL = resultItem.MediumImage.URL;
            }
            else if (resultItem.SmallImage != null)
            {
                Image = new Image();
                Image.Height = resultItem.SmallImage.Height;
                Image.Width = resultItem.SmallImage.Width;
                Image.URL = resultItem.SmallImage.URL;
            }
        }

        //Comparing two objects, the implicit and the past, returning true if they are equal
        public bool EqualsDvd(object value)
        {
            if (value == null) return false;

            if (this.GetType() != value.GetType()) return false;

            AmazonDVD dvd = (AmazonDVD)value;

            if (!Actor.Equals(dvd.Actor)) return false;
            if (!AspectRatio.Equals(dvd.AspectRatio)) return false;
            if (!Audience.Equals(dvd.Audience)) return false;
            if (!Binding.Equals(dvd.Binding)) return false;
            if (!Brand.Equals(dvd.Brand)) return false;
            if (!Condition.Equals(dvd.Condition)) return false;
            if (!Director.Equals(dvd.Director)) return false;
            if (!EAN.Equals(dvd.EAN)) return false;
            if (!Format.Equals(dvd.Format)) return false;
            if (!ItemDimensions.Equals(dvd.ItemDimensions)) return false;
            if (!Label.Equals(dvd.Label)) return false;
            if (!Languages.Equals(dvd.Languages)) return false;
            if (!Price.Equals(dvd.Price)) return false;
            if (!Manufacturer.Equals(dvd.Manufacturer)) return false;
            if (!MPN.Equals(dvd.MPN)) return false;
            if (!NumberOfDiscs.Equals(dvd.NumberOfDiscs)) return false;
            if (!NumberOfItems.Equals(dvd.NumberOfItems)) return false;
            if (!OriginalReleaseDate.Equals(dvd.OriginalReleaseDate)) return false;
            if (!PackageDimensions.Equals(dvd.PackageDimensions)) return false;
            if (!ProductTypeName.Equals(dvd.ProductTypeName)) return false;
            if (!Publisher.Equals(dvd.Publisher)) return false;
            if (!RegionCode.Equals(dvd.RegionCode)) return false;
            if (!ReleaseDate.Equals(dvd.ReleaseDate)) return false;
            if (!RunningTime.Equals(dvd.RunningTime)) return false;
            if (!Studio.Equals(dvd.Studio)) return false;
            if (!TheatricalReleaseDate.Equals(dvd.TheatricalReleaseDate)) return false;
            if (!UPC.Equals(dvd.UPC)) return false;
            if (!Title.Equals(dvd.Title)) return false;

            return true;
        } 
    }
}
